package org.spring.springboot.component;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class SpringExceptionHandler {

    private final static Log log = LogFactory
            .getLog(SpringExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R defaultErrorHandler(HttpServletRequest req, Exception ex) throws Exception {
        log.error(SpringExceptionHandler.class.getName(), ex);
        R result = new R(Type.exception);
        result.setData(ex.toString());
        return result;
    }
}
